//
//  JZDJDViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JZDJDViewController.h"

#import "YJDCell.h"
#import "JZDJDDetViewController.h"
@interface JZDJDViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger pageNo;
    UIImageView *imageView;
    UILabel *labTit;
}

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *array;//数据源

@end

@implementation JZDJDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageNo = 1;
    [self setBGColor];
    [self showNav:@"待接单"];
    [self loadUI];
    [self loadRefresh];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"available"]];
    imageView.hidden = YES;
    [self.view addSubview:imageView];
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
    
    labTit = [[UILabel alloc] init];
    labTit.text = @"暂无数据...";
    labTit.hidden = YES;
    labTit.font = [UIFont systemFontOfSize:15.0];
    labTit.textColor = [UIColor lightGrayColor];
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(imageView.bottom).offset(8);
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    YJDCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YJDCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.labState.hidden = YES;
    cell.labPrice.hidden = YES;
    cell.labPs.hidden = YES;
    
    cell.btnjd.layer.borderWidth = 1.0;
    cell.btnjd.layer.borderColor = [UIColor redColor].CGColor;
    NSDictionary *dic = [_array objectAtIndex:indexPath.row];
    
    cell.labDDH.text = [NSString stringWithFormat:@"订单号：%@",[dic objectForKey:@"ordernumber"]];
    cell.labSth.text = [dic objectForKey:@"ordername"];
    NSString *date = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"addtime"] integerValue]];
    NSString *time = [self getNewDate:date];
    cell.labDate.text = time;
    cell.labPrice.text = [dic objectForKey:@"ordermoney"];
    NSString *orderid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    
    [cell.btnjd setTitle:@"接单" forState:UIControlStateNormal];
    //接单
    cell.right = ^{
        [self alert:orderid];
    };
    
    return cell;
}
//家政确认接单
- (void)alert:(NSString *)strid{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"接单" message:@"是否接受该订单" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setObject:strid forKey:@"id"];//订单id
        NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/receivinghousekeeping",urlHead];
        [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"res:%@",responseObject);
            NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
            if ([error integerValue] == 200) {
                [_tableView.mj_header beginRefreshing];
            }
            
            [LICGToast showWithText:[responseObject objectForKey:@"mssage"]];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
        }];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _array[indexPath.row];
    NSString *orderid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    JZDJDDetViewController *det = [[JZDJDDetViewController alloc] init];
    det.orderId = orderid;
    det.merid = _merid;
    [self.navigationController pushViewController:det animated:YES];
}
//刷新控件
- (void)loadRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //
    [self.tableView.mj_header beginRefreshing];
    //根据位置设置半透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}

//苦力未接单
- (void)loadData{
    [_tableView.mj_footer endRefreshing];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *p = [NSString stringWithFormat:@"%li",pageNo];
    [parmas setObject:p forKey:@"p"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wjhousekeepingorder",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"苦力未接单res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            _array = [responseObject objectForKey:@"result"];
            _tableView.hidden = NO;
            imageView.hidden = YES;
            labTit.hidden = YES;
        }else{
            _tableView.hidden = YES;
            imageView.hidden = NO;
            labTit.hidden = NO;
        }
        [_tableView reloadData];
        //        [self loadUI];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData{
    [_tableView.mj_header endRefreshing];
    pageNo++;
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *p = [NSString stringWithFormat:@"%li",pageNo];
    [parmas setObject:p forKey:@"p"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wjhousekeepingorder",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"苦力未接单res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            [_array addObjectsFromArray:arr];
            NSLog(@"_arrcount:%li",_array.count);
        }
        [_tableView reloadData];
        //[self loadUI];
        
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_footer endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
